package com.rural.bus;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.rural.bus.model.User;
import com.rural.bus.net.MyHttpUtil;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.net.Url;

public class MainActivity extends BaseActivity{
	
	private LinearLayout mLlAtonce,mLlsubsrcibe,mLlSpecRailway,mLlrent;
	private ImageView mIvBanner;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initView();
		setListeners();
		initData();
	}

	@Override
	protected void initView() {
		mBtMiddle.setText("平安用车");
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));
		
		mImgLeft.setVisibility(View.VISIBLE);
		mImgLeft.setBackgroundResource(R.drawable.ic_top_bar_bt);
		
		mBtRight.setVisibility(View.VISIBLE);
		mBtRight.setText("服务订单");
		
		mLlAtonce=(LinearLayout) findViewById(R.id.ll_immed);
		mLlSpecRailway=(LinearLayout) findViewById(R.id.ll_spec_railway);
		mLlrent=(LinearLayout) findViewById(R.id.ll_rent);
		mLlsubsrcibe=(LinearLayout) findViewById(R.id.ll_book);
		
		mIvBanner=(ImageView) findViewById(R.id.iv_banner);

	}

	@Override
	protected void initData() {
		
		if(mCurrentUser!=null){
			update(mCurrentUser);
		}

		MyHttpUtil http=new MyHttpUtil();
		http.addBodyParm("type", "0");
		http.send(Url.banner_url, new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				try {
//					JSONObject jObject=object.getJSONObject("result");
					String url=object.getString("value");
					final String click_url=object.getString("value2");
					mIvBanner.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View arg0) {
//							if(!TextUtils.isEmpty(click_url)){
//								Intent intent=new Intent(MainActivity.this, H5WebActivitiy.class);
//								intent.putExtra("url", click_url);
//								startActivity(intent);
//							}
						}
					});
					bitmapUtils.display(mIvBanner, url,new BitmapLoadCallBack<View>() {

						@Override
						public void onLoadCompleted(View arg0, String arg1,
								Bitmap arg2, BitmapDisplayConfig arg3,
								BitmapLoadFrom arg4) {
							Log.i("load image", "load success");
							mIvBanner.setImageBitmap(arg2);
						}

						@Override
						public void onLoadFailed(View arg0, String arg1,
								Drawable arg2) {
							Log.e("load image", arg1);
						}
					});
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void fail(String error) {
				
			}
		});
	}

	@Override
	protected void setListeners() {
		
		mImgLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startActivity(MineActivity.class);
				overridePendingTransition(R.anim.translate_in_left, R.anim.translate_out_right);
			}
		});
		
		mLlAtonce.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putInt("tag", 0);
				startActivity(AtOnceRailwayActivity.class, bundle);
			}
		});
		mLlsubsrcibe.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putInt("tag", 1);
				startActivity(SubscribeRailwayActivity.class, bundle);
			}
		});
		mLlSpecRailway.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putInt("tag", 2);
				startActivity(SpecRailwayActivity.class, bundle);
			}
		});
		mLlrent.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putInt("tag", 3);
				startActivity(RentRailwayActivity.class,bundle);
			}
		});
		
		mBtRight.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
//				toast("inservice orders");
				startActivity(InServiceActivity.class);
			}
		});

	}
	
	/***
	 * 更新用户的regionId
	 */
	private void update(final User user){
		user.update(this,new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
//				finish();

			}
			
			@Override
			public void fail(String error) {
				toast("更新用户信息失败 "+error);
//				finish();

			}
		});
	}
	
	private long lastTime;
	
	@Override
	public void onBackPressed() {
		if(System.currentTimeMillis() - lastTime > 2000){
			toast("再按一次退出应用");
			lastTime = System.currentTimeMillis();
			return;
		}
		super.onBackPressed();
	}
	
}
